/**
 * 漆水河 脚手架基础公开课
 * 2022/8/13 --- 2022/12/13
 * 有疑问可加QQ群: 809126489，后续会持续输出与大家一起成长
 */
package com.qishuihe.common.domain.role.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qishuihe.baseinterface.constant.CommonConstant;
import com.qishuihe.common.domain.role.service.RoleService;
import com.qishuihe.common.domain.role.entity.RoleEntity;
import com.qishuihe.common.domain.role.mapper.RoleMapper;
import com.qishuihe.baseinterface.basevo.vo.ResultVO;
import com.qishuihe.baseinterface.basevo.vo.ResultPageVO;
import com.qishuihe.outbound.user.QueryPageDTO;
import com.qishuihe.outbound.user.RoleDTO;
import com.qishuihe.baseinterface.utils.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> implements RoleService {

    @Override
    public ResultVO<RoleDTO> modifyRole(RoleDTO roleDTO) {
        ResultVO<RoleDTO> resultDto =  new ResultVO<RoleDTO>();
        RoleEntity roleEntity = new RoleEntity();
        BeanUtils.copyProperties(roleDTO,roleEntity);
        if(!super.saveOrUpdate(roleEntity)){
            resultDto.setCode(CommonConstant.STATUS_ERROR);
            resultDto.setMessage("修改用户失败");
            resultDto.setObj(roleDTO);
        }else {
            BeanUtils.copyProperties(roleEntity, roleDTO);
            resultDto.setObj(roleDTO);
        }
        return resultDto;
    }

    @Override
    public ResultPageVO<RoleDTO> queryRole(QueryPageDTO<RoleDTO> queryPageDTO) {
        RoleDTO roleDTO = queryPageDTO.getParamObj();
        Page<RoleEntity> roleEntityPage= new Page<>();
        BeanUtils.copyProperties(queryPageDTO,roleEntityPage);
        LambdaQueryWrapper<RoleEntity> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(!StringUtil.isEmpty(roleDTO.getName())) {
            lambdaQueryWrapper.like(RoleEntity::getName, roleDTO.getName());
        }
        if(!StringUtil.isEmpty(roleDTO.getCode())) {
            lambdaQueryWrapper.like(RoleEntity::getCode, roleDTO.getCode());
        }
        roleEntityPage =super.page(roleEntityPage,lambdaQueryWrapper);
        ResultPageVO<RoleDTO> resultPageVO = new ResultPageVO<>();
        BeanUtils.copyProperties(roleEntityPage, resultPageVO);
        return resultPageVO;
    }
}
